package com.Gwo.basic.controller;

import com.Gwo.basic.query.BaseQuery;
import com.Gwo.basic.service.IBaseService;
import com.Gwo.basic.utils.PageList;
import com.Gwo.basic.utils.Result;
import com.Gwo.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class BaseController<T> {
    @Autowired
    protected IBaseService<T> baseService;

    @GetMapping("/{id}")
    public T selectById(@PathVariable("id") Long id){
        return baseService.findById(id);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Long id){
        try{
            baseService.delete(id);
            return Result.me().success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.me().fail();
        }
    }

    @PatchMapping
    public Result batchDelete(@RequestBody List<Long> ids){
        try{
            baseService.batchDelete(ids);
            return Result.me().success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.me().fail();
    }
    //分页查询
    @PostMapping
    public PageList selectForPage(@RequestBody BaseQuery query){
        PageList pageList = baseService.queryByPage(query);
        return pageList;
    }

    @GetMapping
    public List<T> selectAll(){
        return baseService.findAll();
    }
}
